<template>
  <div class="mod-config">
    <basic-container>
      <navigation :sign="{a:true,b:false,c:false,d:false,e:false}"></navigation>
      <div class="boxsty zhiBiao">
        <!-- <div class="flex justifyContentSpaceBetween">
          <div class="tabletext">履历信息维护</div>
          <div class="tablelink">
            <el-link icon="el-icon-plus" v-if="permissions.jiangjin_jiangjinryllb_add" type="primary"
              @click="addOrUpdateHandle()" :underline="false">新增人员履历信息</el-link>
          </div>
        </div> -->
        <div :height="searchheight">
              <span
                  style="float: right; margin-top: -30px; cursor: pointer"
                  @click="searchchangeadvanced"
                  >{{ searchadvanced ? "查询条件收起" : "查询条件展开" }}
                  <i
                  :class="
                      searchadvanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
                  "
                  />
              </span>
        <div v-show="searchadvanced">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()" class="form-inline">
          <!-- <el-form-item>
            <div class="block">
              <span class="demonstration">履历年份</span>
              <el-date-picker value-format="yyyy" :picker-options="pickerOptions0" v-model="dataForm.showllYear"
                type="year" placeholder="选择年" style="width: 130px">
              </el-date-picker>
            </div>
          </el-form-item> -->
          <el-form-item label="开始日期">
            <el-date-picker value-format="yyyy-MM-dd" format="yyyy-MM-dd"  v-model="dataForm.llKssj"
                type="date" placeholder="选择日期" style="width: 150px">
              </el-date-picker>
          </el-form-item>
          <el-form-item label="结束日期">
            <el-date-picker value-format="yyyy-MM-dd" format="yyyy-MM-dd"  v-model="dataForm.llJssj"
                type="date" placeholder="选择日期" style="width: 150px">
              </el-date-picker>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="dataForm.ryXm" placeholder="姓名" style="width: 150px;"></el-input>
          </el-form-item>
          <el-form-item label="人员编号">
            <el-input v-model="dataForm.ryCode" placeholder="人员编号" style="width: 150px;"></el-input>
          </el-form-item>
          <el-form-item label="工作单位">
            <el-select ref='multiSelect' @clear="clpar" class="ld-select" clearable :value="valueTitle" filterable
              placeholder="请选择" popper-class="ld-select_tree-drop">
              <el-option :value="valueTitle">
                <el-tree :data="treeData" :props="defaultProps" class="filter-tree" highlight-current accordion
                  @node-click="getNodeData" />
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" class="marginLeft" plain @click="getDataListByYear()">检索</el-button>
          </el-form-item>
          <!-- <el-form-item>
            <el-button type="primary" @click="innerVisible=true" plain>导入</el-button>
          </el-form-item>
          <el-form-item>
            <el-button @click="excelExportHandleDown('人员履历信息')" type="primary" plain>
              导出
            </el-button>
            <el-button @click="temopDow" type="primary" plain>模板下载</el-button>
          </el-form-item> -->
        </el-form>
        </div>
        <div>
          <el-tooltip
                class="item"
                effect="dark"
                content="导入"
                placement="top"
            >
                <el-upload class="upload-demo"  :show-file-list="false"
                  accept=".xlsx, .xls" :headers="headers" action="/admin/sys-file/upload" style="float:left;margin-right:10px;" :on-success="handleUploadSuccess">
                  <el-button  icon="el-icon-upload" type="success" circle></el-button>
                </el-upload>

            </el-tooltip>
            <el-tooltip
                class="item"
                effect="dark"
                content="新增人员履历信息"
                placement="top"
                v-model="isShow"
            >
                <el-button
                icon="el-icon-plus"
                type="primary"
                @click="addOrUpdateHandle()"
                v-if="permissions.jiangjin_jiangjinryllb_add"
                circle
                ></el-button>
            </el-tooltip>
            <el-tooltip
                class="item"
                effect="dark"
                content="导出"
                placement="bottom"
            >
                <el-button type="warning" icon="el-icon-download" @click="excelExportHandleDown('人员履历信息')" circle></el-button>
            </el-tooltip>

<!--            <el-tooltip
                class="item"
                effect="dark"
                content="模板下载"
                placement="top"
            >
                <el-button
                icon="el-icon-download"
                type="info"
                circle
                @click="temopDow"
                ></el-button>
            </el-tooltip>
            <el-tooltip
                class="item"
                effect="dark"
                content="单项奖惩"
                placement="top"
            >
                <el-button
                icon="el-icon-collection"
                type="primary"
                circle
                 @click="btnJc()"
                ></el-button>
            </el-tooltip>-->
            <el-tooltip
                class="item"
                effect="dark"
                content="履历修改"
                placement="top"
            >
                <el-button
                icon="el-icon-pie-chart"
                type="primary"
                circle
                 @click="getRyListByRyCode()"
                ></el-button>
            </el-tooltip>
            <div class="pull-right">
                <el-button type="primary" @click="onConfigure">配置计算时间</el-button>
                <el-button type="warning">获取数据</el-button>
            </div>
        </div>
        <el-table :data="dataList" border v-loading="dataListLoading" @row-dblclick="rowDblclick" stripe ref="table"  width="100%"
          class="avue-crud" min-height="450px" style="margin-top:20px;"header-align="center">
          <el-table-column type="selection" width="55" header-align="center" align="center"></el-table-column>
          <el-table-column prop="" label="序号" width="60" :formatter="nShowIndex"align="center"></el-table-column>
          <el-table-column prop="ryCode" label="人员编号" width="100"align="center">
          </el-table-column>
          <el-table-column prop="ryXm" label="姓名" width="80"align="center">
          </el-table-column>
          <el-table-column prop="llKssj" :formatter="formater" label="开始日期" min-width="100"align="center">
          </el-table-column>
          <el-table-column prop="llJssj" :formatter="formater" label="结束日期" min-width="100"align="center">
          </el-table-column>
          <el-table-column prop="ryGzdw" label="工作单位" show-overflow-tooltip min-width="200"align="center">
          </el-table-column>
          <el-table-column prop="ryZwgz" label="职务或工种" width="150"align="center">
          </el-table-column>
          <el-table-column  prop="zwjb" label="职务级别" width="100"align="center">
          </el-table-column>
          <el-table-column prop="ryXzjb" min-width="120"align="center">
            <template slot="header" slot-scope="scope">
              <b style="color:#000;font-weight: bolder">级别判断</b>
            </template>
            <template slot-scope="scope">
              <div v-if="scope.row.id==dbclickRowId" >
                <el-select v-model="scope.row.ryXzjb" @change="ryXzjbChange(scope.row)" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                  <!-- <el-option style="color:#ff3300" label="领导正职" value="领导正职"></el-option>
                  <el-option label="领导副职" value="领导副职"></el-option>
                  <el-option label="副总师" value="副总师"></el-option> -->
                </el-select>
              </div>
              <span v-else>
                <b v-if="scope.row.zcgzFlag&&scope.row.zcgzFlag==1" style="color:#ff3300">{{scope.row.ryXzjb}}</b>
                <!-- <b v-if="scope.row.ryXzjb=='领导正职'" style="color:#ff3300">{{scope.row.ryXzjb}}</b>-->
                <b v-else>{{scope.row.ryXzjb}}</b> 
                </span>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="jjFfDeptName" label="奖金发放部门名称" show-overflow-tooltip min-width="180"align="center">
          </el-table-column> -->
          <el-table-column prop="jjFfMonth" min-width="100"align="center">
            <template slot="header" slot-scope="scope">
              <b style="color:#000;font-weight: bolder">任职时长(月）</b>
            </template>
            <template slot-scope="scope">
              <b>{{scope.row.jjFfMonth}}</b>
            </template>
          </el-table-column>
          <el-table-column  min-width="100"align="center">
            <template slot="header" slot-scope="scope">
              <b style="color:#000;font-weight: bolder">履历是否连续</b>
            </template>
            <template slot-scope="scope">
              <b v-if="scope.row.llSflx ==0">是</b>
              <el-tooltip v-else class="item" effect="dark" :content="scope.row.sfLxyy" placement="top-start">
                <b style="color:#ff3300" >否</b>
              </el-tooltip>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="llBz" label="备注" min-width="100"align="center">
          </el-table-column> -->
          <el-table-column prop="ryJszc" label="任职文号" min-width="100"align="center">
          </el-table-column>
          <el-table-column label="任免文件上传" width="120" fixed="right" header-align="center" align="center">
            <template slot-scope="scope">
              <el-button type="primary" plain @click="btnAttachment(scope.row)">附件管理</el-button>
                             <!-- <el-button type="primary" plain @click="btnReward(scope.row.ry_code)">奖金查看</el-button>
                             <el-button type="primary" plain @click="btnJc(scope.row.ry_code)">单项奖惩查看</el-button>
                             <el-button type="primary" plain @click="getRyListByRyCode(scope.row.ry_code)">历史履历查看</el-button>
              <el-button type="primary" plain @click="btnJc(scope.row.ry_code)">单项奖惩</el-button>
              <el-button type="primary" plain @click="getRyListByRyCode(scope.row.ry_code)">历史履历</el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <div class="avue-crud__pagination">
          <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background
            layout="total, sizes, prev, pager, next, jumper">
          </el-pagination>
        </div>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
      <errorIndex ref="errorIndex" @refreshDataList="getDataList" v-if="errorIndexV"></errorIndex>
      <liSiRenYuanLieBiao ref="liSiRenYuanLieBiao"  @refreshDataList="getDataList"></liSiRenYuanLieBiao>
      <!-- 单项奖惩查看 -->
      <bonusPenalty v-if="bonusPenaltyVisible" ref="bonusPenalty"></bonusPenalty>
      <!-- 奖金查看  -->
      <reward v-if="rewardVisible" ref="reward"></reward>
      <!-- 附件管理 -->
      <fujian ref='fujian'></fujian>
      </div>
    </basic-container>

    <el-dialog width="40%" title="导入" :visible.sync="innerVisible" append-to-body>
      <el-row class="mb10" style="height: 10vh;" :gutter="10">
        <!-- <el-col :span="5"><span class="demonstration fr">选择年份</span></el-col>
        <el-col :span="15">
          <el-date-picker value-format="yyyy" :picker-options="pickerOptions0" v-model="userYear" type="year"
            placeholder="选择年" class="ml10" style="width:90%">
          </el-date-picker>
        </el-col> -->
        <!-- :before-upload="beforeAvatarUpload" -->
        <el-col :span="4">
          <el-upload class="upload-demo"  :show-file-list="false"
            accept=".xlsx, .xls" :headers="headers" action="/admin/sys-file/upload" :on-success="handleUploadSuccess">
            <el-button type="primary" plain class="fr">导入</el-button>
          </el-upload>
        </el-col>
      </el-row>
    </el-dialog>
    <!--配置计算时间-->
    <el-dialog
      :visible.sync="timeDialogVisible"
      class="timeDialog"
      width="30%"
      >
      <el-col :span="24">
        <el-form ref="timeForm" :model="timeForm" label-width="150px">
          <h6>二级领导任职月份日期</h6>
          <el-col :span="24">
            <el-form-item label="判断任职月份日期">
              <el-input-number v-model="timeForm.rzyfrq" placeholder="":min="1" :max="31"></el-input-number>
            </el-form-item>
          </el-col>
          <h6>配置奖金计算时间</h6>
          <el-col :span="24">
            <el-col :span="12">
              <el-form-item label="开始日期" label-width="100px">
                <el-date-picker
                  v-model="timeForm.importkssj"
                  type="date"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
                  style="width:100%"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="结束日期" label-width="100px">
                <el-date-picker
                  v-model="timeForm.importjssj"
                  type="date"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
                  style="width:100%"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-col>
        </el-form>
      </el-col>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="keepTime">确 定</el-button>
        <el-button @click="timeDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    fetchList,
    delObj,
    saveOrupdate,
    getDatas,
    putObj
  } from '@/api/jiangjin/jiangjinryllb'
  import TableForm from './jiangjinryllb-form'
  import errorIndex from './errorIndex'
  import liSiRenYuanLieBiao from './liSiRenYuanLieBiao'
  import bonusPenalty from '@/components/currency/bonus-penalty';
  import reward from '@/components/currency/reward';
  import fujian from './fujian'
  import store from '@/store'
  import request from '@/router/axios'
  import {
    fetchTree,
    getObj
  } from '@/api/admin/dept'
  import {
    mapGetters
  } from 'vuex'
  export default {
    data() {
      return {
        errorIndexV: false,
        pickerOptions0: {
          disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6
          }
        },
        userYear: '',
        innerVisible: false,
        valueTitle: '',
        headers: {
          Authorization: 'Bearer ' + store.getters.access_token
        },
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        tableDataDept: [],
        dataForm: {
          ryXm: '',
          ryCode: '',
          jjFfDeptCode: '',
          showllYear: new Date(),
          llYear: ''
        },
        dataFormfb:{},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        loading: '',
        bonusPenaltyVisible: false,
        rewardVisible: false,
        searchadvanced: true,
        searchheight: "0",
        isShow:true,
        timeDialogVisible:false,
        timeForm:{},
        options:[
          {
          value: '领导正职',
          label: '领导正职'},
          {
          value: '领导副职',
          label: '领导副职'},
          {
          value: '副总师',
          label: '副总师'},
        ],
        dbclickRowId:""
      }
    },
    components: {
      TableForm,
      errorIndex,
      liSiRenYuanLieBiao,
      bonusPenalty,
      reward,
      fujian
    },
    created() {
      if (this.dataForm.showllYear) {
        var year = new Date(this.dataForm.showllYear).getFullYear();
        this.dataForm.llYear = year;
      }
      this.getDataList();
      fetchTree().then(response => {
        this.treeData = response.data.data
      })
      this.pullgetDatas()
      
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
      pullgetDatas(){
        getDatas().then(res=>{
          console.log("HUOQU:"+JSON.stringify(res))
          if(res.data.data){
            this.timeForm=res.data.data;
          }
        })
      },
      //双击修改
      rowDblclick(row){
        this.dbclickRowId=row.id
      },
      ryXzjbChange(row){
        putObj(row).then(res=>{
          if(res.data.code==0){
            this.$message.closeAll()
            this.$message.success('修改成功!')
            this.getDataList();
            this.dbclickRowId=""
          }
        })
      },
      //配置计算时间
      onConfigure(){
          this.timeDialogVisible=true
         // this.pullgetDatas()
      },
      keepTime(){
        
        saveOrupdate(this.timeForm).then(res=>{
          if(res.data.code==0){
            this.$message.closeAll()
            this.$message.success('保存成功!')
            this.timeDialogVisible=false
            this.pullgetDatas()
          }
        })
      },
      //附件管理
      btnAttachment(row){
        this.$nextTick(() => {
        this.$refs.fujian.init(row.id);
      });
      },
      //检索
      getDataListByYear() {
        if (this.dataForm.showllYear) {
          var year = new Date(this.dataForm.showllYear).getFullYear();
          this.dataForm.llYear = year;
          this.pageIndex = 1;
          this.dataFormfb = JSON.parse(JSON.stringify(this.dataForm));
          this.getDataList();
        } else {
          this.$message.closeAll();
          this.$message.error("请选择导入年份")
        }
      },
      temopDow() {
        window.location.href = '../../static/个人履历信息模板.xls'
      },
      /**
       * 清空下拉
       */
      clpar() {
        this.valueTitle = '';
        this.dataForm.jjFfDeptCode = '';
      },
      /**
       * 根据人员代码查询历史信息
       */
      getRyListByRyCode(ryCode) {
        var data=this.$refs.table.selection;
        if(data.length==0){
          this.$message.closeAll();
          this.$message.error("请选择一条数据！");
          return;
        }
        if (data.length >1) {
          this.$message.closeAll();
          this.$message.error("只能选择一条数据！");
          return;
        }
        if(data.length==1){
          this.$nextTick(()=>{
            this.$refs.liSiRenYuanLieBiao.getDataList(data[0].ryCode,data[0].id)
          })
        }

      },
      /**
       * 下拉选择单机事件
       */
      getNodeData(data) {
        this.valueTitle = data.name;
        getObj(data.id)
          .then(res => {
            var obj = res.data.data;
            this.dataForm.jjFfDeptCode = obj.deptCode
            this.$refs.multiSelect.blur()

          })
          .catch(err => {
           //console.log(err);
          })
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true

        fetchList(Object.assign({
          current: this.pageIndex,
          size: this.pageSize
        }, this.dataFormfb)).then(response => {
          this.dataList = response.data.data.records
          this.totalPage = response.data.data.total
          this.dbclickRowId=""
          this.dataListLoading = false
        }).catch(err =>{
          this.dbclickRowId=""
          this.dataListLoading = false
        })

      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id,this.timeForm)
        })
      },

      nShowIndex(row, column, cellValue, index) {
        return index + 1 + this.pageSize*(this.pageIndex-1);
      },
      //单项奖惩查看
      btnJc() {
        var data=this.$refs.table.selection;
        if(data.length==0){
          this.$message.closeAll();
          this.$message.error("请选择一条数据！");
          return;
        }
        if (data.length >1) {
          this.$message.closeAll();
          this.$message.error("只能选择一条数据！");
          return;
        }
        if(data.length==1){
          this.bonusPenaltyVisible = true;
          this.$nextTick(() => {
            this.$refs.bonusPenalty.init(data[0].ryCode)
          })
        }

      },
      //奖金查看
      btnReward(code) {
        this.rewardVisible = true;
        this.$nextTick(() => {
          this.$refs.reward.init(code)
        })
      },

      /**
       * 文件上传前
       */
      beforeAvatarUpload(file) {
        if (!this.userYear) {
          this.$message.closeAll()
          this.$message.error("请选择导入年份")
          return false;
        }
        this.loading = this.$loading({
          lock: true,
          text: '人员数据验证中...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      },
      /**
       * 文件上传成功
       */
      handleUploadSuccess(response, file, fileList) {
        this.$message.closeAll()
        if (response.code == 0) {
          request({
            url: '/jiangjin/jiangjinryllb/fileDow/' + response.data.bucketName + '/' + response.data.fileName +
              '/' + this.userYear,
            method: 'get',
          }).then((response) => { //
            var obj = response;
            if (obj) {
              this.errorIndexV = true;
             // this.innerVisible = false;
              this.$nextTick(() => {
                this.$refs.errorIndex.getDataList(obj);
                this.loading.close()
               // this.innerVisible = false;
              });
            } else {
              this.loading.close();
              this.errorIndexV = false;
              this.innerVisible = false;
              this.$message.error("数据导入错误请联系管理员")
            }
          }).catch(() => {
            this.loading.close();
            this.errorIndexV = false;
            this.innerVisible = false;

          });

        } else {
          this.loading.close();
          this.errorIndexV = false;
          this.innerVisible = false;
          this.$message.error("数据导入错误请联系管理员")
          this.$message.error('上传出错：' + response.msg)
        }
      },
      formater(row, column, cellValue, index) {
        if (cellValue) {
          return this.$moment(cellValue).format("YYYY-MM-DD");
        } else {
          return cellValue;
        }
      },
      /**
       * excel 导出下载
       */
      excelExportHandleDown(filename) {
        return request({
          url: '/jiangjin/jiangjinryllb/export',
          method: 'post',
          responseType: 'arraybuffer'
        }).then((response) => { // 处理返回的文件流
          const blob = new Blob([response.data], {
            type: 'application/vnd.ms-excel'
          })
          const link = document.createElement('a')
          link.href = URL.createObjectURL(blob)
          link.download = filename
          document.body.appendChild(link)
          link.click()
          window.setTimeout(function () {
            URL.revokeObjectURL(blob)
            document.body.removeChild(link)
          }, 0)
        })
      },
      searchchangeadvanced() {
            this.searchadvanced = !this.searchadvanced;
            if (this.searchadvanced) {
                this.searchheight = "50";
            }
        },

    }
  }

</script>
<style>
.timeDialog .el-dialog__header{
  padding: 0px; 
  padding-bottom: 0px;
}
.timeDialog  h6{
  font-size: 18px;
  font-weight: normal;
  margin-top: 20px;
  margin-bottom: 20px;
}

</style>